// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.itemSetting.propertySetting.relateScreenChoice
{
    import data.vo.LayoutItemVoPropertyPorxy;

    import flash.display.MovieClip;

    import utils.MessageBox;

    public class RelateScreenChoiceComponent
    {
        private var mItemVo:LayoutItemVoPropertyPorxy;

        private var mCurrentSelectedDot:RelateScreenChoiceDotComponent;

        private var mScreenMapping:MovieClip;

        private var mAllRelateSceneChoiceDotComponentVector:Vector.<RelateScreenChoiceDotComponent>;

        public function RelateScreenChoiceComponent()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_screenMappingMc:MovieClip):void
        {
            mScreenMapping = _screenMappingMc;
            mAllRelateSceneChoiceDotComponentVector = new Vector.<RelateScreenChoiceDotComponent>();
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_TOP_LEFT, "topLeft");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_TOP_MIDDLE, "topMiddle");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_TOP_RIGHT, "topRight");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_LEFT_MIDDLE, "leftMiddle");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_MIDDLE, "middle");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_RIGHT_MIDDLE, "rightMiddle");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_BOTTOM_LEFT, "bottomLeft");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_BOTTOM_MIDDLE, "bottomMiddle");
            pushDotComponent(GlobalConfig.RELATIVE_TARGET_SCREEN_BOTTOM_RIGHT, "bottomRight");

            mScreenMapping.visible = false;
        }

        //==================================
        //== Public Function
        //==================================

        public function set enable(_isEnable:Boolean):void
        {
            mScreenMapping.visible = _isEnable;
        }

        public function refresh(_vo:LayoutItemVoPropertyPorxy):void
        {
            mItemVo = _vo;
            for each(var dotComponent:RelateScreenChoiceDotComponent in mAllRelateSceneChoiceDotComponentVector)
            {
                dotComponent.selected = dotComponent.dotID == _vo.relativeTarget;
            }
        }

        public function tryToSaveLayoutItemVo():Boolean
        {
            if (mCurrentSelectedDot != null)
            {
                mItemVo.relativeTarget = mCurrentSelectedDot.dotID;
                return true;
            }
            else
            {
                MessageBox.instance.showMsg("未设置屏幕关联坐标");
                return false;
            }
        }

        public function isRelateTargetIsDotID(_relativeTarget:String):Boolean
        {
            for each(var dotComponent:RelateScreenChoiceDotComponent in mAllRelateSceneChoiceDotComponentVector)
            {
                if (dotComponent.dotID == _relativeTarget)
                {
                    return true;
                }
            }
            return false;
        }


        //==================================
        //== Private Function
        //==================================

        private function pushDotComponent(_dotID:String, _mcKey:String):void
        {
            var dotComponent:RelateScreenChoiceDotComponent = new RelateScreenChoiceDotComponent();
            dotComponent.initialize(mScreenMapping[_mcKey], _dotID, onClickOneOfDotComponent);
            mAllRelateSceneChoiceDotComponentVector.push(dotComponent);
        }

        private function onClickOneOfDotComponent(_target:RelateScreenChoiceDotComponent):void
        {
            for each(var dotComponent:RelateScreenChoiceDotComponent in mAllRelateSceneChoiceDotComponentVector)
            {
                dotComponent.selected = _target == dotComponent;
            }
            mCurrentSelectedDot = _target;
        }

    }
}
